#ifndef __UTIL_HPP__
#define __UTIL_HPP__

#include <iostream>
#include <string>
#include <ctime>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace sqy_log
{
    namespace util
    {
        class Date
        {
            public:
            static time_t now()//获取时间戳
            {
                return time(nullptr);
            }
        };

        class File
        {
            public:
            static bool exists(const std::string& pathname)
            {
                // return access(pathanme.c_str(),F_OK) == 0;//系统调用接口，不具有通用性

                struct stat st;
                if(stat(pathname.c_str(),&st) < 0)
                {
                    return false;
                }
                return true;
            }

            static std::string path(const std::string& pathname)
            {
                size_t pos = pathname.find_last_of("/\\");
                if(pos == std::string::npos)
                {
                    return ".";
                }
                return pathname.substr(0,pos+1);
            }

            static void createDirectory(const std::string& pathname)
            {
                size_t pos = 0, idx = 0;
                while(idx < pathname.size())
                {
                    pos = pathname.find_first_of("/\\",idx);
                    if(pos == std::string::npos)//没找到
                    {
                        mkdir(pathname.c_str(),0777);
                        return;
                    }
                    //找到了
                    std::string parent_dir = pathname.substr(0,pos+1);
                    if(exists(parent_dir))
                    {
                        idx = pos + 1;
                        continue;
                    }

                    mkdir(parent_dir.c_str(),0777);
                    idx = pos + 1;
                }
            }
        };
    }
}
#endif